package com.liuanbang.saro.sarodemo.ui.activity;

import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.liuanbang.saro.sarodemo.R;
import com.liuanbang.saro.sarodemo.base.BaseActivity;
import com.liuanbang.saro.sarodemo.bean.ChartPoint;
import com.liuanbang.saro.sarodemo.util.ToastUtils;
import com.liuanbang.saro.sarodemo.view.CircleBar;
import com.liuanbang.saro.sarodemo.view.CirclePercentView;
import com.liuanbang.saro.sarodemo.view.CustomProgressView;
import com.liuanbang.saro.sarodemo.view.GoodProgressView;
import com.liuanbang.saro.sarodemo.view.LineChartView;
import com.liuanbang.saro.sarodemo.view.ShimmerFrameLayout;
import com.liuanbang.saro.sarodemo.view.VIPView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘安邦 on 2017/3/1.
 * Email:liuanbang@aliyun.com
 * 描述: 控件闪动:
 * 使用教程:http://www.jizhuomi.com/android/example/456.html
 * Git: https://github.com/facebook/shimmer-android 2017/3/1
 * 修改:
 */

public class ViewsActivity extends BaseActivity {
    @Bind(R.id.shimmerFrameLayout1)
    ShimmerFrameLayout shimmerFrameLayout1;
    @Bind(R.id.shimmerFrameLayout2)
    ShimmerFrameLayout shimmerFrameLayout2;
    @Bind(R.id.circlePercentView)
    CirclePercentView circlePercentView;
    @Bind(R.id.lineChartView)
    LineChartView lineChartView;
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.vipView)
    VIPView vipView;
    @Bind(R.id.circleBar)
    CircleBar circleBar;
    @Bind(R.id.progressBar)
    ProgressBar progressBar;
    @Bind(R.id.goodProgress)
    GoodProgressView goodProgress;
    @Bind(R.id.customProgress)
    CustomProgressView customProgress;
    @Bind(R.id.dilogButton)
    Button dilogButton;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_views);
        ButterKnife.bind(this);

        shimmerFrameLayout1.setAutoStart(true);
        shimmerFrameLayout2.setAutoStart(true);

        circlePercentView.setOnCircleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                circlePercentView.setCurPercent((float) (Math.random() * 100));
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> xList = new ArrayList<>();
                List<Integer> yList = new ArrayList<>();
                for (int i = 0; i < 7; i++) {
                    xList.add(i + 1);
                    int y = (int) (Math.random() * 70 + 1);
                    yList.add(y);
                }
                lineChartView.setDataList(xList, yList);
            }
        });

        dilogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSingleChoiceDialog();
            }
        });

        lineChartView.setOnPointClickListener(new LineChartView.OnPointClickListener() {
            @Override
            public void onPointClick(int index, ChartPoint point) {
                ToastUtils.showToast(point.getX() + "--" + point.getY() + "--" + point.getxData() + "--" + point.getyData());
            }
        });

        vipView.startCustomAnimation(6800);

        circleBar.setSweepAngle(56f);
        circleBar.startCustomAnimation();

        progressBar.setProgress(68);

        goodProgress.setProgressValue(50);

        customProgress.setProgressValue(80);

        /**
         * 动画
         */
        final ValueAnimator anim = ValueAnimator.ofInt(0, 80);
        anim.setDuration(2000);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                progressBar.setProgress(value);
                Log.i(TAG, "onAnimationUpdate: " + value);
            }
        });

        anim.start();
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    public Toolbar getToolbar() {
        return null;
    }

    public void clickStart(View view) {
        vipView.setPointAnimator();
    }


    int yourChoice;

    private void showSingleChoiceDialog() {
        final String[] items = {"我是1", "我是2", "我是3", "我是4"};
        yourChoice = -1;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(this);
        singleChoiceDialog.setTitle("我是一个单选Dialog");
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice != -1) {
                            Toast.makeText(ViewsActivity.this,
                                    "你选择了" + items[yourChoice],
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        singleChoiceDialog.show();
    }
}
